package com.yjs.app.trade.transaction.controller;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yjs.app.trade.transaction.util.DESUtils;
import com.yjs.app.trade.transaction.util.HttpUtils;
import com.yjs.app.trade.transaction.util.PropertiesUtils;
import com.yjs.app.trade.transaction.util.Result;
@Controller
@RequestMapping(value = "/invoice")
public class AliInvoiceController{
	@RequestMapping(value = "/findInvoice", method = RequestMethod.POST)
	@ResponseBody
	public Object findInvoice(String datar){
		String invoiceInfo = DESUtils.getDecryptStr(datar, "yjs_trade_app");
		if (invoiceInfo != null && invoiceInfo != "") {
			Map<String, String> map = new HashMap<String, String>();
			String[] invoces = invoiceInfo.split(",");
			String host = PropertiesUtils.getProperty("invoice.host");
			String path = PropertiesUtils.getProperty("invoice.path");
			String method = PropertiesUtils.getProperty("invoice.method");
			String appcode = PropertiesUtils.getProperty("invoice.appcode");
			map.put("fpdm", invoces[2]);// 发票代码
			map.put("fphm", invoces[3]);// 发票号码
			map.put("je", invoces[4]);// 不含税金额
			if(invoces[6]==null || invoces[6].equals("")){
				map.put("jym","0");
			}else{
				map.put("jym", invoces[6].substring(invoces[6].length() - 6, invoces[6].length()));// 校验码后六位
			}		
			map.put("kprq", invoces[5]);// 开票日期
			Map<String, String> headers = new HashMap<String, String>();
			headers.put("Authorization", "APPCODE " + appcode);
			Map<String, String> bodys = new HashMap<String, String>();
			try {
				HttpResponse response = HttpUtils.doPost(host, path, method, headers, map, bodys);
				String result = EntityUtils.toString(response.getEntity());
				return Result.csuccess(result);
			} catch (Exception e) {
				e.printStackTrace();
			}			
		}
		return Result.cfail("扫描数据为空,请重新扫描!");
	}

}
